package com.example.demo6.controller;

/**
 * @Author: Lizhi
 * @Date:Create：in 2023/12/5 9:45
 */
import com.example.demo6.entity.Goods;
import com.example.demo6.entity.Suppliers;
import com.example.demo6.service.SuppliersService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author: Lizhi
 * @Date:Create：in 2023/12/8 17:18
 */
@Controller
@RequestMapping("/suppliers")
public class SuppliersController {
//    1.展示已有数据
    @Autowired
    SuppliersService suppliersService;
    @Autowired
    HttpServletRequest request;
    @RequestMapping("/suppliersList")
    public String suppliersList(Model model){
        Object username=request.getSession().getAttribute("username");
        System.out.println(username);
        if (username==null){
            return "redirect:/";
        }else{
//        1 用service方法查询列表
        List<Suppliers> suppliersList = suppliersService.findAll();
//        Object username=request.getSession().getAttribute("username");

        System.out.println(suppliersList);
//        将查询结果用model绑定
        model.addAttribute("data",suppliersList);
        model.addAttribute("username",username);

//        return视图
        return "suppliers/suppliersList";}
    }
//    2.前端显示增加、删除、更改
    @RequestMapping("/suppliersAdd")
    public String suppliersAdd(Model model){
        Object username=request.getSession().getAttribute("username");
        if (username==null){
            return "redirect:/login";
        }else{
//        Object username=request.getSession().getAttribute("username");
        model.addAttribute("username",username);
        return "suppliersAdd";}
    }
    @RequestMapping("/suppliersAddProcess")
    public String suppliersAddProcess(@RequestParam String name,@RequestParam String addr,@RequestParam String phone){
//1 组织成实体
        Suppliers suppliers = new Suppliers(null,name,addr,phone);

        if (suppliersService.add(suppliers)==1){
            return "redirect:/suppliers/suppliersList";
        }else {
            return "redirect:/suppliers/suppliersList";

        }
    }
    //    处理更新商品，首先要给出一个页面显示该条记录的数据
    @RequestMapping("/suppliersUpdate")
    public String suppliersUpdate(@RequestParam String id,Model model){
        Object username=request.getSession().getAttribute("username");
        if (username==null){
            return "redirect:/login";
        }else{
//        首先根据id查询到该条记录信息
        Suppliers suppliers = new Suppliers();
        suppliers.setId(Integer.valueOf(id));
        Suppliers suppliersdb = suppliersService.findone(suppliers);
//        再将数据绑定到视图
        model.addAttribute("data",suppliersdb);
//        Object username=request.getSession().getAttribute("username");
        model.addAttribute("username",username);

        return "suppliersUpdate";}
    }
}
